﻿

using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.ADO.SqlServer.Dictionary
{

    /// <summary>
    /// 
    /// </summary>
    public class SqlServerStockDao : IStockDao
    {
        /// <summary>
        /// Takes the specified employ test.
        /// </summary>
        /// <param name="stock">The employ test.</param>
        /// <returns></returns>
        private static object[] Take(Stock stock)
        {
            return new object[]  
            {
                            "@StockID" , stock.StockId,
                "@Code" , stock.StockCode,
                "@Name" , stock.StockName,
                "@Description" , stock.Description,
                "@IsActive" , stock.IsActive,
                  "@UnitID" , stock.UnitId
            };
        }
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, Stock> Make = reader =>
          new Stock
          {


                  StockId = reader["StockId"].AsId(),
                     StockCode = reader["Code"].AsString(),
                     StockName = reader["Name"].AsString(),
                     Description = reader["Description"].AsString(),
                     IsActive = reader["IsActive"].AsBool(),
                  UnitId = reader["UnitID"].AsLong()
          };

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="unitId">The unit identifier.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<Stock> GetPaged(int unitId, int currentPage)
        {
            const string sql = @"uspGetPaged_Stock";
            object[] parms = { "@WhereClause", null, "@OrderBy", null, "@PageIndex", currentPage - 1, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// </summary>
        /// <param name="unitId"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public int GetPageSize(int unitId)
        {
            const string sql = @"uspGetPageSize_Stock";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

        /// <summary>
        /// Inserts the stock.
        /// </summary>
        /// <param name="stock">The stock.</param>
        /// <returns></returns>
        public string InsertStock(Stock stock)
        {
            const string sql = "uspInsert_Stock";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(stock));
        }

        /// <summary>
        /// Updates the stock.
        /// </summary>
        /// <param name="stock">The stock.</param>
        /// <returns></returns>
        public string UpdateStock(Stock stock)
        {
            const string sql = "uspUpdate_Stock";
            return Db.Update(sql, CommandType.StoredProcedure, Take(stock));
        }

        /// <summary>
        /// Deletes the stock.
        /// </summary>
        /// <param name="stock">The stock.</param>
        /// <returns></returns>
        public string DeleteStock(Stock stock)
        {
            const string sql = @"uspDelete_Stock";
            object[] parms = { "@StockID", stock.StockId ,
                             "@UnitID", stock.UnitId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }





       public Stock GetStock(int stockId)
        {
            const string sql = @"uspGet_Stock_ByID";
            object[] parms = { "@StockID", stockId };
            return Db.Read(sql, CommandType.StoredProcedure,Make, parms);
        }


        public List<Stock> GetStockByUnit(int unitId)
        {
            const string sql = @"uspGet_Stock_ByUnit";
            object[] parms = { "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }
    }
}
